<template>
  <div class="goodList">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="checkGoods">
            <el-button slot="append" icon="el-icon-search" @click="checkGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column label="ID" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="550px"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="100px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'goodList',
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: '0'
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    getGoodsList() {
      this.$axios
        .get('goods', {
          params: {
            pagenum: this.queryInfo.pagenum,
            pagesize: this.queryInfo.pagesize,
            query: this.queryInfo.query
          }
        })
        .then(result => {
          this.goodsList = result.data.data.goods
          this.total = result.data.data.total
          console.log(result)
        })
    },
    handleEdit(index, row) {
      console.log(row)
    },
    handleDelete(index, row) {
      this.$axios.delete(`goods/${row.goods_id}`).then(result => {
        console.log(result)
        if (result.data.meta.status != 200) {
          this.$message({
            type: 'error',
            message: result.data.meta.msg
          })
          return
        }
        this.$message({
          type: 'success',
          message: result.data.meta.msg
        })
        this.getGoodsList()
      })
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    checkGoods() {
      this.getGoodsList()
    },
    addGoods(){
      this.$router.push({name:'Add'})
    }
  }
}
</script>
<style scoped>
</style>